package chapter04

import scala.math.BigDecimal.double2bigDecimal

object Test02_ForLoop {
  def main(args: Array[String]): Unit = {
    //前后都闭的操作 创建的是一个RichInt的集合
    for (i <- 0 to 3) println("i = " + i)
    println("================================================")
    //前闭合后开
    for (i <- Range(1, 10)) {
      println("i=" + i)
    }
    println("================================================")
    for (i: Int <- 0 until 3) println("i=" + i)
    //集合遍历
    println("================================================")
    for (i <- Array(12, 36, 64)) println("i=  " + i)
    //循环守卫
    println("================================================")
    for (i <- 1 to 10 if i != 5) println("i = " + i)
    //循环步长 step不能为0
    println("================================================")
    for (i <- 1 to 10 by 2) println("i = " + i)
    println("================================================")
    for (i <- 1.0 to 10.0 by 0.5) println("i = " + i)
    println("================================================")
    //循环嵌套
    for (i <- 1 to 9) {
      for (j <- 1 to i) {
        print(s"${i}*${j}=" + (i * j) + "\t\t")
      }
      println()
    }
    println("================================================")
    //循环引入变量
    for (i <- 1 to 10; j = 2 * i + 1) {
      println("i = " + i + " j = " + j)
    }
    println("================================================")
    //循环返回值
    val a: Unit = for (i <- 1 to 10) "hello" //a=()
    println("a=" + a)
    println("================================================")

    def m(n: Int): Int = n * 5

    val value = for (i <- 1 to 10) yield m(i) //产生了一个集合   Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    println(value)
  }
}
